package mfuny.ggongal.admin.http;

import mfuny.ggongal.admin.debug.Debug;
import mfuny.ggongal.admin.https.GGDecoder;
import mfuny.ggongal.admin.https.GGEncoder;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class HttpPvDecoder {
	
	@SuppressWarnings("unchecked")
	public String receiveDecode(String enStr) throws Exception {
		String deStr = GGDecoder.decode(enStr);
		
		JSONObject paramJson = (JSONObject)JSONValue.parse(deStr);
		JSONObject resultJson = new JSONObject();
		
		HttpPv pv = new HttpPv();
		
		//pv 추가.
		if(paramJson.get("tag").equals("pv_add")) {
			try {
				int result = pv.pv_add(
						Integer.parseInt((String)paramJson.get("ad_no")), 
						(String)paramJson.get("pvType")
				);
				if( result <= 0 ) {
					throw new Exception( "error, call pv_add method" );
				}
				resultJson.put("responseCode", result);
				
			} catch ( Exception e ) {
				Debug.toDebugFile(e, paramJson.get("tag") + " / ad_no : " + paramJson.get("ad_no") + " / pvType : " + paramJson.get("pvType"));
				resultJson.put("responseCode", -1);
			}
		}
		
		//pv time 추가.
		else if(paramJson.get("tag").equals("pv_ggongAdd")) {
			try {
				int result = pv.pv_ggongAdd(Integer.parseInt((String)paramJson.get("ad_no")));
				if( result <= 0 ) {
					throw new Exception( "error, call pv_addGgong method" );
				}
				resultJson.put("responseCode", result);
				
			} catch (Exception e) {
				Debug.toDebugFile(e, paramJson.get("tag") + " / ad_no : " + paramJson.get("ad_no") );
			}
		}
		
		return GGEncoder.encodeForJson(resultJson);
	}
}
